import {
  getNoticeList,
  addNotice,
  editNotice,
  delNotice,
  getTemplateParameter,
  getTemplateType,
  startStatus,
  getTemplateListByType,
} from "../server/noticeApi";

const initData = {
  noticeList: [],
  totalCount: 0,
};

export default {
  namespace: "noticeManagement",
  state: {
    ...initData,
  },

  effects: {
    *getNoticeList({ payload }, { call, put }) {
      const { errCode, errortext, datas = [], totalCount } = yield call(
        getNoticeList,
        payload,
      );
      yield put({ type: "get/noticeList", payload: datas, totalCount });
      return { errCode, errortext, datas };
    },
    *addNotice({ payload }, { call }) {
      const resp = yield call(addNotice, payload);
      return resp;
    },
    *editNotice({ payload }, { call }) {
      const resp = yield call(editNotice, payload);
      return resp;
    },
    *delNotice({ payload }, { call }) {
      const { errCode, errortext } = yield call(delNotice, payload);
      return { errCode, errortext };
    },
    *getTemplateType({ payload }, { call }) {
      const { errCode, datas } = yield call(getTemplateType, payload);
      return { errCode, datas };
    },
    *getTemplateParameter({ payload }, { call }) {
      const { errCode, datas } = yield call(getTemplateParameter, payload);
      return { errCode, datas };
    },

    *startStatus({ payload }, { call }) {
      const resp = yield call(startStatus, payload);
      return resp;
    },
    *getTemplateListByType({ payload }, { call }) {
      const { errCode, errortext, datas } = yield call(
        getTemplateListByType,
        payload,
      );
      return { errCode, errortext, datas };
    },
  },
  reducers: {
    "get/noticeList": (state, { payload, totalCount }) => ({
      ...state,
      noticeList: payload,
      totalCount,
    }),
    clearData: () => ({ ...initData }),
  },
};
